package br.com.quad.managedBean.convert;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.quad.bean.Cliente;
import br.com.quad.dao.DAOFactory;

@FacesConverter(value="converteCliente", forClass=Cliente.class)
public class ConverteCliente implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		Cliente cliente;
		if(arg2.equals("0")){
			cliente = new Cliente();
		} else {
			cliente = DAOFactory.getClienteDAO().getCliente(Integer.valueOf(arg2));
		}
		return cliente;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		return String.valueOf(((Cliente) arg2).getCodigoCliente());
	}
}
